package jobhunter.service;

import java.util.GregorianCalendar;

import jobhunter.model.Action;
import jobhunter.model.enums.ActionState;
import jobhunter.model.enums.ActionType;
import jobhunter.model.enums.CommunicationType;
import jobhunter.repository.ActionRepository;
import jobhunter.repository.ContactRepository;
import jobhunter.repository.ProcessRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
 @Transactional
public class ActionService
{

	@Autowired
	private ActionRepository actionRepository;

	@Autowired
	private ProcessRepository processRepository;
	@Autowired
	private ContactRepository contactRepository;
	
	public void add(String name, GregorianCalendar gregorianCalendar,
			int realized, String cv, String motivationLettre,
			String description, ActionType actionType, ActionState actionState,
			CommunicationType communicationType, Long processId, Long contactId)
	{
		Action action = new Action(name, new GregorianCalendar(), realized, cv, motivationLettre, description, actionState, communicationType, actionType);
		action.setProcess(processRepository.findById(processId));
		action.setContact(contactRepository.findById(contactId));
		actionRepository.persist(action);
		
	}

	public void update(Long id, String name,
			GregorianCalendar actionDate, int realized, String cv,
			String motivationLettre, String description, ActionType actionType,
			ActionState actionState, CommunicationType communicationType,
			Long processId, Long contactId)
	{
		Action action = actionRepository.findById(id);
		action.setName(name);
		action.setActionDate(actionDate);
		action.setRealized(realized);
		action.setCv(cv);
		action.setMotivationLettre(motivationLettre);
		action.setDescription(description);
		action.setActionType(actionType);
		action.setState(actionState);
		action.setCommunicationType(communicationType);
		action.setProcess(processRepository.findById(processId));
		action.setContact(contactRepository.findById(contactId));
		actionRepository.merge(action);		
	}

}
